package hos.demo

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import androidx.activity.compose.setContent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ListItem
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.fragment.app.FragmentActivity
import com.blankj.utilcode.util.ToastUtils
import com.blankj.utilcode.util.Utils
import com.blankj.utilcode.util.ZipUtils
import hos.demo.ui.theme.FileTheme
import hos.file.FileResult
import hos.file.FileStart
import hos.file.dialog.FileDialog
import hos.file.dialog.OnImageVideoFileClickListener
import hos.file.provider.FileProviderX
import hos.file.utils.AssetUtils
import hos.file.utils.FileIOUtils
import hos.file.utils.FileUtils
import hos.file.utils.ZipImageUtils
import hos.intent.IntentAvailable
import hos.intent.IntentStart
import hos.permission.PermissionX
import java.io.File
import kotlin.concurrent.thread

class MainActivity : FragmentActivity() {

    private val permissionX = PermissionX.init(this)

    private var createCameraImageUri: Uri? = null

    private var cameraImagePath by mutableStateOf("")
    private var imagePath by mutableStateOf("")
    private var imagePathList by mutableStateOf("")

    private var locationEnabled by mutableStateOf(false)

    @OptIn(ExperimentalMaterial3Api::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Utils.init(application)

        FileProviderX.setAuthority(this, "$packageName.utilcode.fileprovider")

        setContent {
            FileTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {

                    LazyColumn(content = {
                        item {
                            ListItem(
                                headlineText = { Text(text = "处理assets") },
                                modifier = Modifier.clickable {
                                })
                            ListItem(headlineText = {
                                Text(text = "拍照")
                            }, modifier = Modifier.clickable {
                                FileDialog(
                                    this@MainActivity,
                                    FileDialog.TYPE_IMAGE_VIDEO_FILE,
                                    object : OnImageVideoFileClickListener {
                                        override fun onCreateImage(
                                            fileDialog: FileDialog,
                                            context: Context?
                                        ) {
                                            ToastUtils.showLong("onCreateImage")
                                        }

                                        override fun onCreateVideo(
                                            fileDialog: FileDialog,
                                            context: Context?
                                        ) {
                                            ToastUtils.showLong("onCreateVideo")
                                        }

                                        override fun onChooseFile(
                                            fileDialog: FileDialog,
                                            context: Context?
                                        ) {
                                            ToastUtils.showLong("onChooseFile")
                                        }

                                    }).show()
                                // 模拟器不行
                                permissionX.requestCamera {
                                    createCameraImageUri =
                                        FileStart.createImage(this@MainActivity, 100)
                                }
                            })
                            ListItem(headlineText = {
                                Text(text = "录像")
                            }, modifier = Modifier.clickable {
                                // 模拟器不行
                                permissionX.requestAllStorage {
                                    createCameraImageUri =
                                        FileStart.createVideo(this@MainActivity, 100)
                                }
                            })
                            ListItem(headlineText = {
                                Text(text = "录音")
                            }, modifier = Modifier.clickable {
                                permissionX.requestAllStorage {
                                    createCameraImageUri =
                                        FileStart.createAudio(this@MainActivity, 100)
                                }
                            })
                            ListItem(headlineText = {
                                Text(text = "选择图片")
                            }, modifier = Modifier.clickable {
                                permissionX.requestAllStorage {
                                    FileStart.choosePickImage(this@MainActivity, 200)
                                }
                            })
                            ListItem(headlineText = {
                                Text(text = "选择图片和视频")
                            }, modifier = Modifier.clickable {
                                permissionX.requestAllStorage {
                                    FileStart.chooseImageVideo(this@MainActivity, 200)
                                }
                            })
                            ListItem(headlineText = {
                                Text(text = "选择图片,多选")
                            }, modifier = Modifier.clickable {
                                permissionX.requestAllStorage {
                                    FileStart.chooseImageList(this@MainActivity, 300)
                                }
                            })
                            ListItem(headlineText = {
                                Text(text = "选择图片和视频,多选")
                            }, modifier = Modifier.clickable {
                                permissionX.requestAllStorage {
                                    FileStart.chooseImageVideoDocumentList(this@MainActivity, 300)
                                }
                            })
                            ListItem(headlineText = {
                                Text(text = "打开定位设置页面 $locationEnabled")
                            }, modifier = Modifier.clickable {
                                // 检查是否已经打开系统定位
                                locationEnabled =
                                    IntentStart.checkLocationSetting(this@MainActivity, 400)
                                if (locationEnabled) {
                                    // 已经打开系统定位
                                    Log.d("RESULT", "locationEnabled 已经打开系统定位")
                                }
                            })
                            ListItem(headlineText = {
                                Text(
                                    text = "cameraImagePath $cameraImagePath",
                                    modifier = Modifier.clickable {
                                        FileStart.openFile(this@MainActivity, cameraImagePath, 500)
                                    })
                            })
                            ListItem(headlineText = {
                                Text(text = "imagePath $imagePath",
                                    modifier = Modifier.clickable {
                                        FileStart.openFile(this@MainActivity, imagePath, 500)
                                    })
                            })
                            ListItem(headlineText = {
                                Text(text = "imagePathList $imagePathList")
                            })
                        }
                    })
                }
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100) {
            val cameraFile = FileResult.getMedia(this, resultCode, data, createCameraImageUri)
            if (cameraFile == null) {
                cameraImagePath = ""
                Log.d("RESULT", "onActivityResult: null")
                return
            }
            thread {
//                val cameraFileSize = FileUriUtils.fileSize(cameraFile)
                val fileZipImage = ZipImageUtils.fileZipImage(this, cameraFile)
//                val fileSize = FileUriUtils.fileSize(fileZipImage)
                runOnUiThread {
                    cameraImagePath = fileZipImage.absolutePath
                    Log.d("RESULT", "onActivityResult: $cameraImagePath")
                }
            }

        }
        if (requestCode == 200) {
            val cameraFile = FileResult.getFile(this, resultCode, data)
            if (cameraFile == null) {
                imagePath = "";
                Log.d("RESULT", "onActivityResult: null")
                return
            }
            imagePath = cameraFile.absolutePath
            Log.d("RESULT", "onActivityResult: ${cameraFile.absolutePath}")
        }
        if (requestCode == 300) {
            val cameraFile = FileResult.getFileList(this, resultCode, data)
            if (cameraFile == null) {
                imagePathList = ""
                Log.d("RESULT", "onActivityResult: null")
                return
            }
            imagePathList = ""
            cameraFile.forEach {
                imagePathList += it.absolutePath
                Log.d("RESULT", "onActivityResult: ${it.absolutePath}")
            }
        }
        if (requestCode == 400) {
            locationEnabled = IntentAvailable.locationAvailable(this)
            Log.d("RESULT", "locationEnabled onActivityResult: $locationEnabled")
        }
    }


}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    FileTheme {
        Greeting("Android")
    }
}